package com.atguigu.tingshu.dispatch.job;
import java.util.Date;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @XxlJob(value = "updateLatelyAlbumRankingJobhandler")
    public void updateLatelyAlbumRanking(){
        XxlJobLog xxlJobLog = new XxlJobLog();
        long jobId = XxlJobHelper.getJobId();
        xxlJobLog.setJobId(jobId);
        long startTime = System.currentTimeMillis();
        Result result = searchFeignClient.updateLatelyAlbumRanking();
        if(result.getCode() == 200){
            xxlJobLog.setStatus(1);
        }else{
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(result.getMessage());
        }
        long endTime = System.currentTimeMillis();

        xxlJobLog.setTimes((int)(endTime - startTime));
        xxlJobLogMapper.insert(xxlJobLog);
        log.info("排行榜数据跟新成功了");

    }
    @XxlJob(value = "updateVipExpireStatusJob")
    public void updateVipExpireStatusJob(){
        XxlJobLog xxlJobLog = new XxlJobLog();
        long jobId = XxlJobHelper.getJobId();
        xxlJobLog.setJobId(jobId);
        long startTime = System.currentTimeMillis();
        Result result = userInfoFeignClient.updateVipExpireStatus();
        if(result.getCode() == 200){
            xxlJobLog.setStatus(1);
        }else{
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(result.getMessage());
        }
        long endTime = System.currentTimeMillis();

        xxlJobLog.setTimes((int)(endTime - startTime));
        xxlJobLogMapper.insert(xxlJobLog);
        log.info("更新Vip到期失效状态成功了");

    }
}